package mttestclient;

import javax.swing.*;
import java.awt.event.*;

/**
 *
 * @author Stewart Gibson 0705777
 * 
 * This class is used to both control the GUI components and to call the 
 * TestModel methods in when they are required.  All 3 classes will work
 * together to create a way of stressing the voting server that is to be created
 * by the others.
 */

public class TestViewController {
    
    View v;
    TestModel tm;    
    
            
    private int TIMER_DELAY = 500;
    private Timer sliderUpdateTimer;
    private Timer textAreaUpdateTimer;
    
    public TestViewController(View view, TestModel testmodel){
        v = view;
        tm = testmodel;        
                        
        addListeners();
        startSliderTimer();
        
        v.setVisible(true);
    }
    
    
    /*The following methods are used to add listeners to teh start and stop 
     * buttons that can be found in the GUI.
     */
    private void addListeners(){       
        v.jButtonStart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startStressTest();    //Begins the stress testing
            }
        });       
        
        v.jButtonStop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tm.setContinuousTesting(false);
                v.jButtonStart.setEnabled(true);
                v.jButtonStop.setEnabled(false);
                v.jSliderNoClientConnections.setEnabled(true);
                startSliderTimer();
            }
        });  
    }
    
    
    private void startStressTest(){
        v.jButtonStart.setEnabled(false);
        v.jButtonStop.setEnabled(true);
        v.jSliderNoClientConnections.setEnabled(false);
        stopSliderTimer();
        v.jTextAreaTestData.append("Awaiting connection... \n");
        
        
        //Retrieve the information to be used in the stress test
         
        tm.setConnectionStartAddress(v.getjTextFieldServerIP().getText());
        tm.setConnectionPort((Integer) (v.getjSpinnerPort().getValue()));
        tm.setContinuousTesting(((String) (v.getjComboBoxVoteType().
                getSelectedItem())).contains("Continuous") );
        tm.setSynchroniseClients(((String) (v.getjComboBoxVoteType().
                getSelectedItem())).contains("Synchronised") );
        tm.setOffsetQuantity((Integer) (v.getjSpinnerTimeout().getValue()));
        String[] options = v.getjTextAreaVoteStrings().getText().
                split("\\r?\\n");
        
        tm.getOptions().clear();
        for (int i=0; i < options.length; i++) {
            String option = options[i].trim();
            if (option.length() > 0) {
                tm.getOptions().add(option);
            }
        }
        
        tm.startTest(getNoClients());

        startTextTimer();
    }

    //These Methods are used to keep the value of 
    //the slider constantly up to date
    private void startSliderTimer() {
        sliderUpdateTimer = new Timer(TIMER_DELAY, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateSliderValue();
            }
        });
        sliderUpdateTimer.start(); // start timer
    }
    
    // Stops the Timer.
    private void stopSliderTimer() {
        sliderUpdateTimer.stop();
    }
    
    private void updateSliderValue(){
        
        int x = v.jSliderNoClientConnections.getValue();
        String value = Integer.toString(x);
        
        v.jLabelnoSelected.setText(value);
    }
    //These Methods are used to keep the value of the 
    //slider constantly up to date
   
    private void startTextTimer() {
        textAreaUpdateTimer = new Timer(TIMER_DELAY, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateTextAreaValue();
            }
        });
        textAreaUpdateTimer.start(); // start timer
    }
        
    // Stops the Timer.
    private void stopTextTimer() {
        textAreaUpdateTimer.stop();
    }
    
    private void updateTextAreaValue(){
        String output = tm.getClientInfo(); 
        if (output.length() > v.jTextAreaTestData.getText().length())
            v.jTextAreaTestData.setText(tm.getClientInfo());
    }
    
    //Returns the value of the slider 
    private int getNoClients(){
        
       int x = v.jSliderNoClientConnections.getValue();
       
       return x;
    }
}


